package com.yasaka.stock.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.http.HttpEntity;
import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.net.URLDecoder;
import java.util.ArrayList;

@Api(value = "remote", tags = {"远程服务"})
@RestController
public class RemoteController {

    @Value("${remote.urlA}")
    public String urlA;
    @Value("${remote.urlB}")
    public String urlB;
    @Value("${remote.urlC}")
    public String urlC;

    @ApiOperation(value = "字数统计", notes = "传入文本内容获取字数统计结果")
    @RequestMapping(value = "/countWithTxt", method = RequestMethod.POST)
    public String countWithTxt(@ApiParam(name = "content", value = "文本内容", required = true) @RequestParam String content,
                               @ApiParam(name = "from", value = "源语种", required = true) @RequestParam String from,
                               @ApiParam(name = "userID", value = "用户标识", required = true) @RequestParam String userID,
                               @ApiParam(name = "sID", value = "系统标识", required = true) @RequestParam String sID) {
        CloseableHttpClient httpclient = null;
        HttpPost httppost = null;
        String result = "";
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httppost = new HttpPost(urlA);
            ArrayList<NameValuePair> nvpairs = new ArrayList<NameValuePair>();
            nvpairs.add(new BasicNameValuePair("method", "CountWithTxt"));
            nvpairs.add(new BasicNameValuePair("content", content));
            nvpairs.add(new BasicNameValuePair("from", from));
            nvpairs.add(new BasicNameValuePair("userID", userID));
            nvpairs.add(new BasicNameValuePair("sID", sID));
            httppost.setEntity(new UrlEncodedFormEntity(nvpairs, "UTF-8"));
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                result = URLDecoder.decode(EntityUtils.toString(resEntity), "UTF-8");
            } else {
                throw new IllegalStateException("countWithTxt failed: " + response.getStatusLine());
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httppost.releaseConnection();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    @ApiOperation(value = "文件字数统计", notes = "传入json内容获取文件字数统计结果")
    @RequestMapping(value = "/advancedCountWords", method = RequestMethod.POST)
    public String advancedCountWords(@ApiParam(name = "data", value = "json内容", required = true) @RequestParam String data) {
        CloseableHttpClient httpclient = null;
        HttpPost httppost = null;
        String result = "";
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httppost = new HttpPost(urlA);
            ArrayList<NameValuePair> nvpairs = new ArrayList<NameValuePair>();
            nvpairs.add(new BasicNameValuePair("method", "AdvancedCountWords"));
            nvpairs.add(new BasicNameValuePair("data", data));
            httppost.setEntity(new UrlEncodedFormEntity(nvpairs, "UTF-8"));
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                result = URLDecoder.decode(EntityUtils.toString(resEntity), "UTF-8");
            } else {
                throw new IllegalStateException("advancedCountWords failed: " + response.getStatusLine());
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httppost.releaseConnection();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    @ApiOperation(value = "PDF转换为WORD", notes = "传入json内容获取文件转换结果")
    @RequestMapping(value = "/peConvertPdf", method = RequestMethod.POST)
    public String peConvertPdf(@ApiParam(name = "MissionID", value = "任务编号", required = true) @RequestParam String MissionID,
                               @ApiParam(name = "From", value = "系统标识", required = true) @RequestParam String From,
                               @ApiParam(name = "FileDataList", value = "文件列表", required = true) @RequestParam String FileDataList) {
        CloseableHttpClient httpclient = null;
        HttpPost httppost = null;
        String result = "";
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httppost = new HttpPost(urlB);
            ArrayList<NameValuePair> nvpairs = new ArrayList<NameValuePair>();
            final String fileDataFormat = "{'From':'%s','FileDataList':%s,'MissionID':'%s'}";//json内容
            String data = String.format(fileDataFormat,From,FileDataList,MissionID);
            System.out.println(data);
            nvpairs.add(new BasicNameValuePair("method", "PeConvertPdf"));
            nvpairs.add(new BasicNameValuePair("data", data));
            httppost.setEntity(new UrlEncodedFormEntity(nvpairs, "UTF-8"));
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                result = URLDecoder.decode(EntityUtils.toString(resEntity), "UTF-8");
            } else {
                throw new IllegalStateException("peConvertPdf failed: " + response.getStatusLine());
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httppost.releaseConnection();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }


    @ApiOperation(value = "在线预览", notes = "传入json内容获取文件预览地址")
    @RequestMapping(value = "/getPreview", method = RequestMethod.POST)
    public String getPreview(@ApiParam(name = "From", value = "系统标识", required = true) @RequestParam String From,
                           @ApiParam(name = "FileData", value = "文件列表", required = true) @RequestParam String FileData) {
        CloseableHttpClient httpclient = null;
        HttpPost httppost = null;
        String result = "";
        CloseableHttpResponse response = null;
        try {
            httpclient = HttpClients.createDefault();
            httppost = new HttpPost(urlC);
            ArrayList<NameValuePair> nvpairs = new ArrayList<NameValuePair>();
            nvpairs.add(new BasicNameValuePair("method", "GetPreview"));
            final String fileDataFormat = "{'From':'%s','FileData':%s}";//json内容
            String data = String.format(fileDataFormat,From,FileData);
            nvpairs.add(new BasicNameValuePair("data", data));
            httppost.setEntity(new UrlEncodedFormEntity(nvpairs, "UTF-8"));
            response = httpclient.execute(httppost);
            int statusCode = response.getStatusLine().getStatusCode();
            if (statusCode == HttpStatus.SC_OK) {
                HttpEntity resEntity = response.getEntity();
                result = URLDecoder.decode(EntityUtils.toString(resEntity), "UTF-8");
            } else {
                throw new IllegalStateException("getPreview failed: " + response.getStatusLine());
            }
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            httppost.releaseConnection();
            if (response != null) {
                try {
                    response.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}
